package com.neu.cloudfactory.controler;

import com.neu.cloudfactory.bean.Capacity.Capactity;
import com.neu.cloudfactory.bean.Equipement.Equipment;
import com.neu.cloudfactory.bean.Order.Ordering;
import com.neu.cloudfactory.bean.Scheduling.Scheduling;
import com.neu.cloudfactory.bean.Scheduling.SchedulingTableData;
import com.neu.cloudfactory.bean.Tender.Tender;
import com.neu.cloudfactory.repository.*;
import org.hibernate.criterion.Order;
import org.hibernate.sql.ordering.antlr.OrderingSpecification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RestController
public class SchedulingControler {
    @Autowired
    SchedulingRepository schedulingRepository;
    @Autowired
    CapactityRepository capactityRepository;
    @Autowired
    EquipmentRepository equipmentRepository;
    @Autowired
    OrderRepository orderRepository;
    @Autowired
    TenderRepository tenderRepository;

    @RequestMapping("/scheduling/getTableData/{orderid}")
    public List<SchedulingTableData> getTableData(@PathVariable("orderid") Integer orderid){
        List<SchedulingTableData> back = new ArrayList<>();
        SchedulingTableData tmp;
        Equipment equipment;
        try{
            Ordering order = orderRepository.getById(orderid);
            Tender tender = tenderRepository.getById(order.getTenderid());
            List<Capactity> capactities = capactityRepository.findAllByFactoryidAndProductid(tender.getFactoryid(),order.getProductid());
            for (Capactity capactity : capactities) {
                equipment =equipmentRepository.getById(capactity.getEquipmentid());

                if (equipment.getStatusid()==1){
                    tmp = new SchedulingTableData();
                    tmp.setEquimentid(equipment.getId());
                    tmp.setEuipmentname(equipment.getName());
                    tmp.setNum(capactity.getNum());

                    back.add(tmp);
                }
            }
        }catch (Exception e){
        }
        return back;
    }

    @RequestMapping("/scheduling/addscheduling")
    public String addscheduling(@RequestBody Scheduling scheduling){
        boolean complete = false;
        try{
            Ordering ordering = orderRepository.getById(scheduling.getOrderid());
            if (ordering.getStatusid()==4){
                ordering.setStatusid(5);
                orderRepository.save(ordering);
                tenderRepository.deleteAllByOrderidAndStatusid(ordering.getId(),1);
            }
            scheduling = schedulingRepository.save(scheduling);
            Equipment equipment=equipmentRepository.getById(scheduling.getEquipmentid());
            equipment.setStatusid(2);
            equipment = equipmentRepository.save(equipment);

            complete = scheduling.getId()!=null && equipment.getStatusid()==2;
        }catch (Exception e){

        }

        return complete?"success":"failed";
    }

    @RequestMapping("/scheduling/finish/{equipmentid}")
    public String finish(@PathVariable("equipmentid") Integer equipmentid){
        boolean complete = false;
        try{
            Scheduling scheduling = schedulingRepository.findByEquipmentid(equipmentid);
            Equipment equipment = equipmentRepository.getById(scheduling.getEquipmentid());
            equipment.setStatusid(1);
            equipmentRepository.save(equipment);

            List<Scheduling> tmp = schedulingRepository.findAllByOrderid(scheduling.getOrderid());
            if(tmp.isEmpty()){
                Ordering ordering = orderRepository.getById(scheduling.getOrderid());
                ordering.setStatusid(5);
                ordering = orderRepository.save(ordering);
            }
            schedulingRepository.delete(scheduling);

            complete = true;
        }catch (Exception e){
            System.out.println(e);
        }

        return complete?"success":"failed";
    }

}
